package guanqu.server;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.beans.IrrBAWaterGate;
import guanqu.beans.IrrBRStadia;
import guanqu.beans.IrrBTCanalDetail;
import guanqu.server.GetStadia.Download;
import helpers.BeanHelper;
import manager.PersistentManager;

/**
 * 获取所有的基本信息 Servlet implementation class GetBasicInfo
 */
// @WebServlet("/GetBasicInfo")
public class GetBasicInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetBasicInfo() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// response.getWriter().append("Served at:
		// ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");

		// 下行数据
		Download download = new Download();

		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.getCurrentSession();

		Transaction tx = session.beginTransaction();
		try {
			// 获取测站信息
			Query q1 = session.createQuery("from IrrBRStadia");
			List<IrrBRStadia> irrBRStadias = q1.list();
			download.setIrrBRStadias(irrBRStadias);

			// 获取渠道信息
			Query q2 = session.createQuery("from IrrBTCanalDetail");
			List<IrrBTCanalDetail> details = q2.list();

			// for(IrrBTCanalDetail d:details)
			// {
			// Query temp = session.createQuery("from IrrBRStadia stadia where
			// stadia.locCode = ?");
			// temp.setParameter(0, d.getCanalCode());
			// List<IrrBRStadia> stadias = temp.list();
			// if (stadias != null && !stadias.isEmpty()) {
			// d.setStadiaCode(stadias.get(0).getStadiaCode());
			// }
			//
			// }

			download.setIrrBTCanalDetails(details);

			// 获取闸门，包含测站信息
//			Query q3 = session.createQuery("from IrrBAWaterGate");
//
//			List<IrrBAWaterGate> irrBAWaterGates = q3.list();
//			for (IrrBAWaterGate g : irrBAWaterGates) {
//				Query qtemp = session.createQuery("from IrrBRStadia stadia where stadia.locCode = ?");
//				qtemp.setParameter(0, g.getWatergateCode());
//				List<IrrBRStadia> stadias = qtemp.list();
//				if (stadias != null && !stadias.isEmpty()) {
//					g.setStadiaCode(stadias.get(0).getStadiaCode());
//				}
//			}
			
			List<IrrBAWaterGate> irrBAWaterGates = new ArrayList<IrrBAWaterGate>();
			
			Query q3 = session.createQuery("select g.watergateCode,g.canalCode,g.watergateName,g.watergateType,g.watergateMET,g.watergateLC,g.watergateNUM,g.watergateHeight,g.watergateWtdth,g.watergateQ,g.watergateMTP,g.watergateStatus,g.engManCd,g.telephone,g.timeSign,g.remark,g.designWaterLevel,stdia.stadiaCode from IrrBAWaterGate as g,IrrBRStadia as stdia"
					+ " where stdia.locCode = g.watergateCode");
			List results = q3.list();
			for(Object o:results)
			{
				Object[] colums = (Object[]) o;
				IrrBAWaterGate temp = new IrrBAWaterGate();
				temp.setWatergateCode((String) colums[0]);
				temp.setCanalCode((String) colums[1]);
				temp.setWatergateName((String) colums[2]);
				temp.setWatergateType((String) colums[3]);
				temp.setWatergateMET((String) colums[4]);
				temp.setWatergateLC((String) colums[5]);
				temp.setWatergateNUM((BigDecimal) colums[6]);
				temp.setWatergateHeight((BigDecimal) colums[7]);
				temp.setWatergateWtdth((BigDecimal) colums[8]);
				temp.setWatergateQ((BigDecimal) colums[9]);
				temp.setWatergateMTP((BigDecimal) colums[10]);
				temp.setWatergateStatus((Character) colums[11]);
				temp.setEngManCd((String) colums[12]);
				temp.setTelephone((String) colums[13]);
				temp.setTimeSign((Calendar) colums[14]);
				temp.setRemark((String) colums[15]);
				temp.setDesignWaterLevel((BigDecimal) colums[16]);
				temp.setStadiaCode((String) colums[17]);
				irrBAWaterGates.add(temp);
			}

			download.setIrrBAWaterGates(irrBAWaterGates);

			download.setStatus(DownloadInfo.SUCCESS);
			tx.commit();
		} catch (Exception e) {
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		} finally {
//			session.close();
		}
		try {
			// 去掉多余空格
			for (IrrBAWaterGate waterGate : download.getIrrBAWaterGates()) {
				BeanHelper.beanAttributeValueTrim(waterGate);
			}
			for (IrrBRStadia stadia:download.getIrrBRStadias())
			{
				BeanHelper.beanAttributeValueTrim(stadia);
			}
			for (IrrBTCanalDetail detail: download.getIrrBTCanalDetails())
			{
				BeanHelper.beanAttributeValueTrim(detail);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.setCharacterEncoding("UTF-8");
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	// 下行数据
	public static class Download {
		int status;
		// 渠道基本信息
		List<IrrBTCanalDetail> irrBTCanalDetails = new ArrayList<>();

		// 测站信息
		List<IrrBRStadia> irrBRStadias = new ArrayList<>();

		// 水闸信息
		List<IrrBAWaterGate> irrBAWaterGates = new ArrayList<>();

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public List<IrrBTCanalDetail> getIrrBTCanalDetails() {
			return irrBTCanalDetails;
		}

		public void setIrrBTCanalDetails(List<IrrBTCanalDetail> irrBTCanalDetails) {
			this.irrBTCanalDetails = irrBTCanalDetails;
		}

		public List<IrrBRStadia> getIrrBRStadias() {
			return irrBRStadias;
		}

		public void setIrrBRStadias(List<IrrBRStadia> irrBRStadias) {
			this.irrBRStadias = irrBRStadias;
		}

		public List<IrrBAWaterGate> getIrrBAWaterGates() {
			return irrBAWaterGates;
		}

		public void setIrrBAWaterGates(List<IrrBAWaterGate> irrBAWaterGates) {
			this.irrBAWaterGates = irrBAWaterGates;
		}

	}

}
